package com.zzk.www.tool.common;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggerFactory;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by zzk on 2017/12/21.
 */
public class BeanUtils {

    private static final Logger log = Logger.getLogger(BeanUtils.class);

    public BeanUtils() {
    }


    public static List<Map<String, Object>> beanListToMapList(List<? extends Object> beanList) {
        List<Map<String, Object>> mapBeanList = new ArrayList();
        if(CollectionUtils.isEmpty(beanList)) {
            return mapBeanList;
        } else {
            Iterator i$ = beanList.iterator();

            while(i$.hasNext()) {
                Object object = i$.next();

                try {
                    mapBeanList.add(PropertyUtils.describe(object));
                } catch (Exception var5) {
                    log.error("bean to queryMap failed! ", var5);
                }
            }

            return mapBeanList;
        }
    }

    public static Map<String, Object> beanToMap(Object bean) {
        try {
            return PropertyUtils.describe(bean);
        } catch (Exception var2) {
            log.error("bean to queryMap failed! ", var2);
            return null;
        }
    }

    public static void mapToBean(Map<String, Object> map, Object bean) {
        try {
            org.apache.commons.beanutils.BeanUtils.populate(bean, map);
        } catch (Exception var3) {
            log.error("bean to queryMap failed! ", var3);
        }

    }

    public static <T> T beanCopy(Object srcBean, Class<T> destClass) {
        if(srcBean == null) {
            return null;
        } else {
            try {
                T destObj = destClass.newInstance();
                PropertyUtils.copyProperties(destObj, srcBean);
                return destObj;
            } catch (Exception var3) {
                throw new RuntimeException("bean copy error! ", var3);
            }
        }
    }
}
